package com.clc.controller.captcha;

import java.awt.image.BufferedImage;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.clc.comm.captcha.LoginImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

@Controller
@RequestMapping("test")
public class CaptchaConterller {
	
	static Log log = LogFactory.getLog(CaptchaConterller.class.getName());
	private ImageCaptchaService service = LoginImageCaptchaService
			.getInstance();
	
	@RequestMapping(value="captchaInit.do")
	public void init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.setHeader("Cache-Control", "no-store");
			response.setHeader("Pragma", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("image/jpeg");
			String captchaId = request.getSession().getId();
			BufferedImage challenge = service.getImageChallengeForID(captchaId);
			//验证 验证码是否正确
			//LoginImageCaptchaService.getInstance().validateResponseForID(captchaId.toString(), "输入的字符串");
	        OutputStream ops=response.getOutputStream();
	        javax.imageio.ImageIO.write(challenge, "jpeg", ops);
	        ops.flush();
	        ops.close();
	        log.info("验证码已经生成:"+captchaId);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
